#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<cstring>
#include"Logger.hpp"

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " serverip serverport" << std::endl;
}

// ./dict_client serverip serverport
int main(int argc , char* argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        exit(0);
    }

    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);

    //创建套接字
    int sockfd = socket(AF_INET , SOCK_DGRAM , 0);
    if(sockfd < 0)
    {
        std::cout<< "create socket error" << std::endl;
        return 0;
    }
    LOG(LogLevel::DEBUG)<<"create socket success";

    //客户端的bind 不用我们自己显式地处理
    struct sockaddr_in server;
    memset(&server , 0 , sizeof(server));//将结构体置空
    //填写数据
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());

    while(true)
    {
        std::cout<< "Please Enter@ ";
        std::string line;
        std::getline(std::cin , line);
        
        //写
        //#include<sys/socket.h>        ssize_t sendto(int sockfd, const void buf[.len], size_t len, int flags,
        //const struct sockaddr *dest_addr, socklen_t addrlen);
        sendto(sockfd , line.c_str() , line.size() ,0 , (struct sockaddr*)&server , sizeof(server));
        // std::cout<<"写成功"<<std::endl;

        //读
        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        char buffer[1024];
        int m = recvfrom(sockfd , buffer , sizeof(buffer) , 0, (struct sockaddr*)&temp , &len);
        if(m > 0)
        {
            buffer[m] =  0 ;
            std::cout<<buffer << std::endl;
        }
        // std::cout<<"读成功"<<std::endl;
    }
    return 0;
}